/*
 * @(#)SpringBeanFactory.java $version 2012-3-2
 * 
 * Copyright 2011 RogueDevelopment Gruop. All rights Reserved.
 * ROGUEDEVELOPMENT PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package com.nadostar.extensions.util;

import org.springframework.context.ApplicationContext;

/**
 * @author LEESUNG
 * 
 */
public class SpringBeanFactory {
	private static ApplicationContext applicationContext;

	/**
	 * Spring의 {@link ApplicationContext}를 설정한다.
	 * 
	 * @param applicationContext
	 *            ApplicationContext
	 */
	public static void init(ApplicationContext applicationContext) {
		SpringBeanFactory.applicationContext = applicationContext;
	}

	/**
	 * Spring의 {@link ApplicationContext}를 소멸시킨다.
	 */
	public static void clear() {
		if (applicationContext != null) {
			applicationContext = null;
		}
	}

	/**
	 * Spring의 {@link ApplicationContext}를 통해 Bean을 반환한다.
	 * 
	 * @param name
	 *            String
	 * @return Object
	 */
	public static Object getBean(String name) {
		if (applicationContext == null) {
			throw new IllegalStateException(
					"ApplicationContext is null. "
							+ "Try to initialize SpringBeanFactory using 'container' element in configuration");
		}

		return applicationContext.getBean(name);
	}

	/**
	 * Spring {@link ApplicationContext}의 초기화 여부을 반환한다.
	 * 
	 * @return 초기화 여부
	 */
	public static boolean isInitialized() {
		return applicationContext != null;
	}
}
